package cn.gson.school.controller.xitong;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.conmmon.JsonResult;
import cn.gson.school.dao.xitong.ResourcesDao;
import cn.gson.school.dao.xitong.RoleSerchDao;
import cn.gson.school.entity.xitong.ResourcesEntity;
import cn.gson.school.entity.xitong.RoleEntity;
import cn.gson.school.mappers.xitong.RoleSerchMapper;
import lombok.extern.log4j.Log4j;

@Controller
@Log4j
@RequestMapping("/role/manager")
public class RoleSerchController {

	@Autowired
	private RoleSerchMapper roleSerchMapper;
	@Autowired
	private RoleSerchDao roleSerchDao;

	@SuppressWarnings("unused")
	@Autowired
	private ResourcesDao resourcesDao;

	@RequestMapping
	public String index() {
		return "xitong/xt-role-list";
	}
	/** 
	 * 查询所有数据
	 * 
	 * @param role
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> roleselect(RoleEntity role, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<RoleEntity> pageData = roleSerchMapper.rolelist(role);
		System.out.println(roleSerchMapper.rolelist(role));
		PageInfo<RoleEntity> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;

	}

	/**

	 * 删除角色

	 * @param id

	 * @return

	 */
	@GetMapping("/delete")
	@ResponseBody
	@Transactional
	public JsonResult delete(Long id) {
		try {
			// 先删除用户-角色中间表

			roleSerchMapper.deleteByRoleId(id);
			RoleEntity role = roleSerchDao.findOne(id);
			if (role != null) {
				role.setResources(null);
				roleSerchDao.save(role);
				roleSerchDao.delete(role);
			}
			return JsonResult.success();
		} catch (Exception e) {
			return JsonResult.error(e.getMessage());
		}
	}

	
	@RequestMapping({ "/form", "/load" })
	public String form(Long id, Model model) {
		if (id != null) {
			// 编辑

			RoleEntity role = roleSerchDao.findOne(id);
			model.addAttribute("role", role);
		}
		return "xitong/xt_role_form";
	}

	@PostMapping({ "/save", "/update" })
	@ResponseBody
	@Transactional
	public JsonResult form(@Valid RoleEntity role) {
		try {
			roleSerchDao.save(role);
			return JsonResult.success();
		} catch (Exception e) {
			return JsonResult.error("校验不通过！");
		}
	}
	
	/**
	 * 获取角色对应的权限列表
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("role/resource/{id}")
	@ResponseBody
	public List<ResourcesEntity> permission(@PathVariable("id") Long id) {
		RoleEntity role = roleSerchDao.findOne(id);
		List<ResourcesEntity> set=role.getResources();
		System.out.println(set);
		List<ResourcesEntity> list = new ArrayList<ResourcesEntity>(set);
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getEnable() == false) {
				list.remove(i);
			}
		}
		return list;
	}

	@RequestMapping("role/resource/tree")
	@ResponseBody
	public List<ResourcesEntity> permissionTree() {
		return resourcesDao.findAllByEnableAndParentIsNull(true);
	}

	/**

	 * 授权

	 * 

	 * @param roleId

	 * @param permissionId

	 * @return

	 */
	@RequestMapping("resource/save")
	@Transactional
	@ResponseBody
	public JsonResult permissionSave(Long roleId, Long[] resourceId) {
		RoleEntity role = roleSerchDao.findOne(roleId);
		// 先清除已有角色

		role.getResources().clear();
		for (Long pid : resourceId) {
			role.getResources().add(resourcesDao.findOne(pid));
		}

		roleSerchDao.save(role);

		return JsonResult.success("授权成功！");
	}
}
